layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 营业额报备详情表管理
     */
    var BtyMdYydzDetail = {
        tableId: "btyMdYydzDetailTable"
    };

    /**
     * 检索报备状态
     */
    var languageAjax = new $ax(Feng.ctxPath + "/rest/dict/listDictsByCode?dictTypeCode=md_report_type");
    var languageResult = languageAjax.start();
    $.each(languageResult.data, function (index, val) {
        $("#reportType").append("<option value='" + val.code + "'>" + val.name + "</option>");
    });
    form.render('select');


    /**
     * 初始化表格的列
     */
    BtyMdYydzDetail.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers',align:'center', title: '序号'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'code', hide: true, title: '门店编号'},
            {field: 'yydzId', hide: true, sort: true, title: '营业对账id'},
            // {field: 'promoterType', sort: true, title: '发起人:0->店长,1->应收会计'},
            {field: 'reportType', align:'center', title: '店长报备状态', templet: function (d) {
                if (d.reportType == "0"){
                    return "点错单";
                }else if (d.reportType == "1"){
                    return "会员卡充值退款";
                }else if (d.reportType == "2"){
                    return "其他";
                }else if (d.reportType == "" || d.reportType == undefined || d.reportType == null){
                    return "";
                }
             }},
            {field: 'dzApply', align:'center', title: '店长报备问题'},
            {field: 'cwApprove', align:'center', title: '财务审核情况',edit:'text'},
            {field: 'dzReplay', align:'center', title: '店长回复审核情况'},
            {field: 'finalMethod', align:'center', title: '最终解决方式',edit:'text'},
            {field: 'status', align:'center', title: '状态', templet: function (d) {
                let msg = "";
                if (d.dzMsg == 0 && d.ddMsg == 0){
                    msg = "未发送";
                }else if (d.dzMsg == 1 && d.ddMsg == 0){
                    msg = "已发送店长";
                }else if (d.dzMsg == 0 && d.ddMsg == 1){
                    msg = "已发送督导";
                }else if (d.dzMsg == 1 && d.ddMsg == 1){
                    msg = "<div class='div'>已发送店长<div>已发送督导</div></div>";
                }

                return msg;
            }},
            // {field: 'dzMsg', align:'center', title: '是否发送店长消息;0->未发送1->已发送'},
            // {field: 'ddMsg', align:'center', title: '是否发送督导消息;0->未发送1->已发送'},
            // {field: 'dzApplyTime', sort: true, title: '店长报备时间'},
            // {field: 'cwApproveTime', sort: true, title: '财务审核时间'},
            // {field: 'dzReplayTime', sort: true, title: '店长回复时间'},
            // {field: 'createTime', sort: true, title: '创建时间'},
            // {field: 'createUser', sort: true, title: '创建者'},
            // {field: 'updateTime', sort: true, title: '修改时间'},
            // {field: 'updateUser', sort: true, title: '修改人'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyMdYydzDetail.search = function () {
        var queryData = {};

        queryData['reportType'] = $("#reportType").val();
        table.reload(BtyMdYydzDetail.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    //监听单元格编辑
    table.on('edit(btyMdYydzDetailTable)', function(obj){
        //得到修改后的值
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段

        $.ajax({
            type: "PUT",
            url: "/btyMdYydzDetail/editItem",
            datatype: "json",
            data: data,
            success: function (data) {
                table.reload(BtyMdYydzDetail.tableId);
            }, error: function () {
                // Feng.error("修改失败！");
            }, complete: function (XHR, TS) { }
        });
    });

    /**
     * 弹出添加对话框
     */
    BtyMdYydzDetail.openAddDlg = function () {
        func.open({
            height: 320,
            width:'500px',
            title: '添加营业额报备详情表',
            content: Feng.ctxPath + '/btyMdYydzDetail/add?yydzId=' + Feng.getUrlParam("yydzId") + '&code=' + Feng.getUrlParam("code"),
            tableId: BtyMdYydzDetail.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      BtyMdYydzDetail.openEditDlg = function (data) {
          func.open({
              title: '修改营业额报备详情表',
              content: Feng.ctxPath + '/btyMdYydzDetail/edit?id=' + data.id,
              tableId: BtyMdYydzDetail.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    BtyMdYydzDetail.exportExcel = function () {
        var checkRows = table.checkStatus(BtyMdYydzDetail.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 推送店长消息
     * @param data
     */
    BtyMdYydzDetail.sendDzItem = function (data){
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyMdYydzDetail/pushDzMsg", function (data) {
                Feng.success("发送成功!");
                table.reload(BtyMdYydzDetail.tableId);
            }, function (data) {
                Feng.error("发送失败!" + data.responseJSON.message + "!");
            });
            ajax.set(data);
            ajax.set("dzMsg", 1);
            ajax.start();
        };
        Feng.confirm("是否发送?", operation);

    };

    /**
     * 推送督导消息
     * @param data
     */
    BtyMdYydzDetail.sendDdItem = function (data){
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyMdYydzDetail/pushDdMsg", function (data) {
                Feng.success("发送成功!");
                table.reload(BtyMdYydzDetail.tableId);
            }, function (data) {
                Feng.error("发送失败!" + data.responseJSON.message + "!");
            });
            ajax.set(data);
            ajax.set("ddMsg", 1);
            ajax.start();
        };
        Feng.confirm("是否发送?", operation);

    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyMdYydzDetail.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyMdYydzDetail/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyMdYydzDetail.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyMdYydzDetail.tableId,
        url: Feng.ctxPath + '/btyMdYydzDetail/list?yydzId=' + Feng.getUrlParam("yydzId")+ '&date=' + Feng.getUrlParam("date"),
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: BtyMdYydzDetail.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyMdYydzDetail.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyMdYydzDetail.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyMdYydzDetail.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyMdYydzDetail.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyMdYydzDetail.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyMdYydzDetail.onDeleteItem(data);
        }else if (layEvent === 'sendDz') {
            BtyMdYydzDetail.sendDzItem(data);
        }else if (layEvent === 'sendDd') {
            BtyMdYydzDetail.sendDdItem(data);
        }
    });
});
